//路由鉴权文件，校验是否登录，路由是否有权限访问等
import router from "@/router";
//引入项目配置，实现修改页面标题
import setting from "./setting";
import pinia from "./stores";
import useRouteStore from './stores/modules/routes'
let routeStore = useRouteStore(pinia);

// 免登录及免动态路由名单
let whiteList = ['/404']

//全局前置守卫
router.beforeEach((to, from, next) => {
  document.title = `${setting.title}-${to.meta.title}`;
  if (whiteList.indexOf(to.path) !== -1) {
    // 在免登录免动态白名单，直接进入
    next()
  }

  if(!routeStore.routes){
    routeStore.addRoutes().then(r=>{
      r.forEach(route=>{
        router.addRoute(route)
      })
      next({...to})
    }).catch(e=>{
      // 当未获取到服务器动态路由时进入
      console.log('错误',e)
      next({ path: '/404' })
    })
  }else{
    next()
  }
});

//全局后置守卫
router.afterEach((to, from) => {
  //进度条结束
  // nprogress.done();
});
